package cn.com.lsy.product.lepao.service.impl;

import cn.com.lsy.product.lepao.model.dao.LpDeviceCodeTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpEquipmentInfoTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpEquipmentLogsTblMapper;
import cn.com.lsy.product.lepao.model.entities.*;
import cn.com.lsy.product.lepao.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceService implements IDeviceService {

    @Autowired
    LpEquipmentInfoTblMapper lpEquipmentInfoTblMapper;
    @Autowired
    LpDeviceCodeTblMapper lpDeviceCodeTblMapper;
    @Autowired
    LpEquipmentLogsTblMapper lpEquipmentLogsTblMapper;

    @Override
    public List<LpEquipmentInfoTbl> selectStoreDeviceList(int storeId) {

        LpEquipmentInfoTblExample example = new LpEquipmentInfoTblExample();
        example.createCriteria().andStoreIdEqualTo(storeId);

        return lpEquipmentInfoTblMapper.selectByExample(example);
    }

    @Override
    public LpEquipmentInfoTbl selectDeviceInfo(int id) {

        return lpEquipmentInfoTblMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateDevice(LpEquipmentInfoTbl param) {

        if (lpEquipmentInfoTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public List<LpEquipmentInfoTbl> selectStoreDeviceListByDevId(String deviceId) {

        LpEquipmentInfoTblExample example = new LpEquipmentInfoTblExample();
        example.createCriteria().andEquipmentIdEqualTo(deviceId);

        return lpEquipmentInfoTblMapper.selectByExample(example);
    }

    @Override
    public boolean addDevice(LpEquipmentInfoTbl param, String uuid) {

        if (lpEquipmentInfoTblMapper.insertSelective(param) > 0) {

            //写入映射关系
            LpDeviceCodeTbl lpDeviceCodeTbl = new LpDeviceCodeTbl();
            lpDeviceCodeTbl.setCode(uuid);
            lpDeviceCodeTbl.setDeviceid(param.getEquipmentId());
            lpDeviceCodeTblMapper.insertSelective(lpDeviceCodeTbl);

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean delDevice(int id) {

        if (lpEquipmentInfoTblMapper.deleteByPrimaryKey(id) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean clearStateOrUserId(int storeId) {

        LpEquipmentInfoTblExample example = new LpEquipmentInfoTblExample();
        example.createCriteria().andStoreIdEqualTo(storeId);

        LpEquipmentInfoTbl lpEquipmentInfoTbl = new LpEquipmentInfoTbl();
        lpEquipmentInfoTbl.setState("0");
        lpEquipmentInfoTbl.setUseUserId(0);

        if (lpEquipmentInfoTblMapper.updateByExampleSelective(lpEquipmentInfoTbl, example) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public List<LpEquipmentLogsTbl> selectEquipmentLogsByUser(int userId) {

        LpEquipmentLogsTblExample example = new LpEquipmentLogsTblExample();
        example.createCriteria().andUserIdEqualTo(userId);

        example.setOrderByClause("id desc");

        return lpEquipmentLogsTblMapper.selectByExample(example);

    }
}
